#include "ObjMesh.h"
#include "IndexBuffer.h"
#include "VertexBuffer.h"
#include "TriangleList.h"
#include "FXParameter.h"
#include "DAEFloat.h"
#include "Skeleton.h"

ObjMesh::ObjMesh():
m_pVertexList(NULL),
m_AABBMesh(NULL),
m_pSkeleton(NULL)
{
}

ObjMesh::ObjMesh(VertexList* vl,IndexedGeometry* ig)
	:
	m_pVertexList(vl),
	m_AABBMesh(NULL),
	m_pSkeleton(NULL)
{
	IndexBufferMaterial* ibm = new IndexBufferMaterial(ig);
	m_IndexBuffers.push_back(ibm);
}

ObjMesh::ObjMesh(VertexList * vl,IndexedGeometry * ig, Material* pMaterial)
	:
	m_pVertexList(vl),
	m_AABBMesh(NULL),
	m_pSkeleton(NULL)
{
	IndexBufferMaterial* ibm = new IndexBufferMaterial(ig,pMaterial);
	m_IndexBuffers.push_back(ibm);
}

ObjMesh::~ObjMesh(void)
{
	SAFE_DELETE(m_pVertexList);
	vector<IndexBufferMaterial*>::const_iterator it;
	for (it = m_IndexBuffers.begin();it!=m_IndexBuffers.end();++it)
		delete *it;
	SAFE_DELETE(m_AABBMesh);
	//SAFE_DELETE(m_pSkeleton);
}

void ObjMesh::SetSkeleton(Skeleton* skeleton){
		m_pSkeleton = skeleton;
		
}

void ObjMesh::SetName(tstring name){
	m_Name = name;
}

tstring ObjMesh::GetName(){
	return m_Name;
}

bool ObjMesh::NeedsTangents()
{
	vector<IndexBufferMaterial*>::const_iterator it;
	for ( it = m_IndexBuffers.begin(); it != m_IndexBuffers.end();++it)
	{
		IndexBufferMaterial* ibm = *it;
		if (ibm->GetMaterial() ==NULL)
			continue;
		else{
			if ( ibm->GetMaterial()->IsNormalMapTextureSet())
				return true;
		}
	}
	return false;
}